<?php

namespace App\Http\Controllers;

use App\CrmProduct;
use App\Rule;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

use App\CrmProductCategory;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
    public $rule=array();
    /**
     * Controller constructor.
     * @param $table_id
     */
    public function __construct($table_id = null)
    {
        $this->middleware(function ($request, $next)use($table_id) {
            #获取角色id
            $role_id = session('user.role_id');
            #获取授权信息
            $rule = Rule::access($role_id,$table_id);
            if($rule){
                $this->rule['view'] = $rule['view'];
                $this->rule['add'] = $rule['add'];
                $this->rule['update'] = $rule['update'];
                $this->rule['delete'] = $rule['delete'];
            }else{
                $this->rule['view'] = 0;
                $this->rule['add'] = 0;
                $this->rule['update'] = 0;
                $this->rule['delete'] = 0;
            }
            return $next($request);
        });
    }

    /**
     * 获取树状分类
     *
     * @return mixed
     */
    public function productCate() {
        #获取产品一级分类
        $res = CrmProductCategory::where('level',1)->where('status',1)->get()->toArray();
        foreach ($res as $one => $cateOne) {
            #获取产品二级分类
            $res[$one]['underling'] = CrmProductCategory::where('parent_id',$cateOne['id'])->where('status',1)->get()->toArray();
            foreach ($res[$one]['underling'] as $two=>$cateTwo){
                $res[$one]['underling'][$two]['underling'] = CrmProduct::where('category',$cateTwo['id'])->where('status',1)->get()->toArray();
            }
        }
        return $res;
    }
    /**
     * 判断是否是移动设备
     *
     * @return bool
     */
    public function is_mobile()
    {
        $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
        $is_pc = (strpos($agent, 'windows nt')) ? true : false;
        $is_mac = (strpos($agent, 'mac os')) ? true : false;
        $is_iphone = (strpos($agent, 'iphone')) ? true : false;
        $is_android = (strpos($agent, 'android')) ? true : false;
        $is_ipad = (strpos($agent, 'ipad')) ? true : false;


        if ($is_pc) {
            return false;
        }

        if ($is_mac) {
            return false;
//            return true;
        }

        if ($is_iphone) {
            return true;
        }

        if ($is_android) {
            return true;
        }

        if ($is_ipad) {
            return true;
        }
    }
}
